package dkbta.processing.tasks;

import dkbta.ontology.elements.ElementDef;
import dkbta.processing.Patients;
import dkbta.processing.WorkQueue;
import dkbta.util.Pair;

/**
 * A task that is used for computing elements depending on a group of patients (like
 * statistical abstractions).
 * 
 * @author Uri Kanonov
 */
public class MultiplePatientTask extends Task{
	/** A reference to the patient container */
	protected final Patients _patients;

	/** The ranges of patient ids associated with this task */
	protected final Pair<Integer, Integer>[] _idRanges;

	/**
	 * Initializes a multiple-patient local computation task
	 * 
	 * @param element The associated element
	 * @param patients A reference to the patient container
	 * @param idRanges The ranges of patient ids associated with this task
	 */
	public MultiplePatientTask(ElementDef element, Patients patients,
		Pair<Integer, Integer>[] idRanges){
		super(element, -1, true);
		_patients = patients;
		_idRanges = idRanges;
	}

	@Override
	public boolean performTask(WorkQueue wq){
		boolean completed = element.compute(_patients, _idRanges, wq, this);
		if (completed){
			// Notifying all parent tasks that this child task has been completed
			notifyParentTasksAboutCompletion(wq);

			// Noting that the element has been computed
			_patients.addComputedElement(element);
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public String toString(){
		return "Multi: PID = " + _patientId + ", " + element + " "
				+ ((isPrimary) ? "[PRIMARY]" : "");
	}
}
